
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Friendster.Service.Core;
using Friendster.Service;

namespace Friendster.Web
{
    /// <summary>
    /// This custom control is the gateway to Friendster Developer platform and responsible for establishing
    /// session and getting tokane from the Friendster. Each application need to initialize this control
    /// before start working with the Friendster Developer platform.
    /// </summary>
    [DefaultProperty("ID")]
    public class FriendsterApplication : WebControl
    {
        //Fields 
        private string _applicationKey;
        private string _applicationName;
        private string _callbackUrl;
        private string _canvasUrl;
        private string _secret;
        private FriendsterService _service;
        private static string _session;
        private static string _userID;
        private bool _enableExternalBrowsing;

        // Properties
        /// <summary>
        /// API Key, provided by Friendster while registering the application.
        /// </summary>
        [Category("Behavior"), DefaultValue(""), Bindable(true)]
        public string ApplicationKey 
        {
            get
            {
                return this._applicationKey;
            }
            set
            {
                this._applicationKey = value;
            }
        }
        /// <summary>
        /// Name of the Application.
        /// </summary>
        [DefaultValue(""), Category("Behavior")]
        public string ApplicationName 
        {
            get
            {
                return this._applicationName;
            }
            set
            {
                this._applicationName = value;
            }
        }
        /// <summary>
        /// Callback URL of the application.
        /// </summary>
        [DefaultValue(""), Category("Behavior")]
        public string CallbackUrl 
        {
            get
            {
                return this._callbackUrl;
            }
            set
            {
                this._callbackUrl = value;
            }
        }
        /// <summary>
        /// Canvas URL of the application.
        /// </summary>
        [DefaultValue(""), Category("Behavior")]
        public string CanvasUrl 
        {
            get
            {
                return this._canvasUrl;
            }
            set
            {
                if (value == "")
                    this._canvasUrl = "http://widgets.friendster.com/" + this._applicationKey;
                else
                    this._canvasUrl = value;
            }
        }
        /// <summary>
        /// Secret Key, provided by Friendster while registering the application.
        /// </summary>
        [DefaultValue(""), Category("Behavior"), Bindable(true)]
        public string Secret 
        {
            get
            {
                return this._secret;
            }
            set
            {
                this._secret = value;
            }
        }
        /// <summary>
        /// Current session key value, established by this control.
        /// </summary>
        public string Session 
        {
            get
            {
                return _session;
            }
        }
        /// <summary>
        /// User ID according to current established session
        /// </summary>
        public string UserID 
        {
            get
            {
                return _userID;
            }
            set
            {
                _userID = value;
            }
        }
        /// <summary>
        /// Represents whether the application runs inside or outside the Friendster
        /// </summary>
        public bool EnableExternalBrowsing 
        {
            get
            {
                return this._enableExternalBrowsing;
            }
            set
            {
                this._enableExternalBrowsing = value;
            }
        }
        /// <summary>
        /// Instance of FriendsterService class, for using with this control
        /// </summary>
        public FriendsterService Service
        {
            get
            {
                return this._service;
            }
        }

        //Methods
        /// <summary>
        /// Constructor method. This will create token and establish session, if not set already.
        /// </summary>
        public FriendsterApplication()
        {
            if (HttpContext.Current == null)
                return;
            if (string.IsNullOrEmpty(_session))
            {
                FriendsterRequest freq = new FriendsterRequest(this.Session);
                _session = freq.Session;
            }
            this._service = new FriendsterService(_session);
        }

        /// <summary>
        /// This method will redirect browser to given url
        /// </summary>
        /// <param name="url">string</param>
      
        public void Redirect(string url)
        {
            HttpResponse response = this.Page.Response;          
            if (!url.StartsWith("http://"))
                url = "http://" + url;
            
            if (this._enableExternalBrowsing)
                response.Redirect(url);
            else
                response.Write(string.Format("<script type=\"text/javascript\">window.top.location.href='{0}';</script>",url));

            response.End();
        }
        /// <summary>
        /// Writing header info inorder to work the redirection in framed pages also.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!this._enableExternalBrowsing)
            {
                this.Page.Response.AppendHeader("P3P", "CP=\"CAO PSA OUR\"");
            }
        }
        /// <summary>
        /// This method has overwritten to bypass the design time error of control.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current==null)
                return;
            base.Render(writer);
        }
   }
}